﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility.Model;
using System.Text;
using Utility;
using Hxj.Data;
using System.Data;
using Utility.Json;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_User 的摘要说明
    /// </summary>
    public class Handler_User : BaseUI.BaseHandler
    {
        #region 保存用户权限
        /// <summary>
        /// 保存用户权限
        /// </summary>
        public void SaveUserAuth()
        {
            string SelectAuth = ParamsOfRequest.Request("SelectAuth");
            string strUserID = ParamsOfRequest.Request("UserID");
            try
            {
                BLL.BLLServiceHelper.GetUserService().SaveUserAuth(strUserID, SelectAuth);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
        #endregion

        #region 创建用户权限数
        /// <summary>
        /// 创建用户权限数
        /// </summary>
        public void CreateUserAuth()
        {
            string reulst = string.Empty;
            string UserID = ParamsOfRequest.Request("UserID");
            using (DataSet ds = BLL.BLLServiceHelper.GetUserService().GetAllList(UserID))
            {
                DataTable moduleStr = ds.Tables[0];
                DataTable moduleTable_YX = ds.Tables[1];
                DataTable moduleOperateStr = ds.Tables[2];
                DataTable moduleOperateTable_YX = ds.Tables[3];
                TreeItem ti;
                string strEmp = Guid.Empty.ToString();
                ti = new TreeItem() { id = strEmp + "_0", text = "所有系统模块列表", value = strEmp, isexpand = true };
                JsonRoleAuthTree.GetTreeModuleItemFromDataTable(ti, moduleStr, moduleTable_YX, moduleOperateStr, moduleOperateTable_YX, "0");
                reulst = ti.ToJsonString();
                moduleStr = moduleTable_YX = moduleOperateStr = moduleOperateTable_YX = null;
            }
            base.Response.Write(reulst);
        }
        #endregion

        #region 获取用户列表分页
        /// <summary>
        /// 获取用户列表
        /// </summary>
        public void GetUserList()
        {
            WhereClip wcp = new WhereClip();
            var pageConditon = GetPageConditon(wcp);
            string strWhere = pageConditon.ComplexSearchCondition.Where.Replace("CreateDate", "Sys_User.CreateDate");
            wcp = new WhereClip();
            wcp = wcp.And(Sys_User._.IsDelete == false);
            wcp = wcp.And(strWhere);
            pageConditon.ComplexSearchCondition = wcp;
            JsonJqGridData gridList = BLL.BLLServiceHelper.GetUserService().GetUserList(pageConditon);
            string strResult = gridList.ToJsonString();
            base.Response.Write(strResult);
        }
        #endregion

        #region 删除用户
        /// <summary>
        /// 删除
        /// </summary>
        public void Delete()
        {
            string strID = ParamsOfRequest.Request("UserID");
            try
            {
                BLL.BLLServiceHelper.GetUserService().Delete(strID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
        #endregion

        #region 获取分组没有分组的用户
        /// <summary>
        /// 获取用户列表
        /// </summary>
        public void GetUserALLList()
        {
            WhereClip wcp = new WhereClip();
            wcp = wcp.And(Sys_User._.IsDelete == false);
            string strGroupID = ParamsOfRequest.Request("GroupID");
            if (!string.IsNullOrEmpty(strGroupID))
            {
                wcp = wcp.And(Sys_User._.UserID.SubQueryNotIn(DbSession.Default.From<Sys_GroupUser>().Where(Sys_GroupUser._.GroupID == strGroupID).Select(Sys_GroupUser._.UserID)));
            }
            string strDeptGuID = ParamsOfRequest.Request("DeptGuID");
            if (!strDeptGuID.IsNullOrEmpty())
            {
                wcp = wcp.And(Sys_User._.DeptGuID == strDeptGuID);
            }
            string strSortFiled = ParamsOfRequest.jqGridSortField;
            string strSortBy = ParamsOfRequest.jqGridSortBy;
            OrderByClip OrderBy = null;
            if (strSortFiled.Length > 0)
            {
                if (String.Equals(strSortBy, "desc", StringComparison.CurrentCultureIgnoreCase))
                {
                    OrderBy = new OrderByClip(strSortFiled, OrderByOperater.DESC);
                }
                else
                {
                    OrderBy = new OrderByClip(strSortFiled, OrderByOperater.ASC);
                }
            }
            using (DataTable dt = BLL.BLLServiceHelper.GetUserService().GetUserTable(wcp, OrderBy))
            {
                JsonJqGridData grid = new JsonJqGridData();
                grid.page = 0;
                grid.records = int.MaxValue;
                grid.total = int.MaxValue;
                grid.rows = dt;
                string strResult = grid.ToJsonString();
                base.Response.Write(strResult);
            }
        }
        #endregion

        public void GetUserDrop()
        {
            List<Sys_User> list = BLL.BLLServiceHelper.GetBaseDataService().GetList<Sys_User>(Sys_User._.IsDelete == false);
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            if (list.Count > 0)
            {
                foreach (Sys_User model in list)
                {
                    sb.Append("{\"UserName\":\"" + model.UserName + "\",\"Name\":\"" + model.Name + "\"},");
                }
                sb.Remove(sb.Length - 1, 1);
            }
            sb.Append("]");
            base.Response.Write(sb.ToString());
        }
    }
}